package com.cy.ppj.service.message.impl;

import com.cy.ppj.dao.mapper.message.MessageRecordMapper;
import com.cy.ppj.dao.mapper.productannouncement.ProductAnnouncementMapper;
import com.cy.ppj.dao.mapper.user.UserMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.user.User;
import com.cy.ppj.model.enums.ProductAndAnnouncementType;
import com.cy.ppj.model.query.message.MessageRecordQuery;
import com.cy.ppj.model.vo.front.message.MessageIndexVO;
import com.cy.ppj.model.vo.front.message.MessageRecordVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.message.MessageRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 消息记录
 * @author: Ocean
 * @create: 2018-09-18 10:11
 **/
@Slf4j
@Service
public class MessageRecordServiceImpl extends BaseService implements MessageRecordService {

    @Autowired
    private MessageRecordMapper messageRecordMapper;
    @Autowired 
    private ProductAnnouncementMapper productAnnouncementMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void addMessageRecord(MessageRecord messageRecord) {
        messageRecordMapper.insertSelective(messageRecord);
    }

    @Override
    @Transactional
    public void addMessageRecords(List<MessageRecord> messageRecords) {
        messageRecordMapper.insertBatch(messageRecords);
    }

	@Override
	public PageBean<MessageRecordVO> selectMessageRecordList(MessageRecordQuery query) {
		int total = messageRecordMapper.selectMessageRecordCount(query);
		List<MessageRecordVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = messageRecordMapper.selectMessageRecordList(query);
		}
		
		return new PageBean<>(list, total);
	}

	@Override
	public List<MessageIndexVO> selectMessageIndexVOs(Long userId) {
		
		User user = userMapper.selectByPrimaryKey(userId);
		state(user != null, "用户数据异常");
		Date lastReadProductNoticeTime = user.getLastReadProductNoticeTime();
		Date lastReadPlatformAnnouncementTime = user.getLastReadPlatformAnnouncementTime();
		Date nowTime = new Date();
		int unReadCount = 0;
		// 个人消息
		MessageIndexVO personalMessageRecord = messageRecordMapper.selectMessageIndexInfo(userId);
		if (personalMessageRecord != null) {
			personalMessageRecord.setType(1);
		}else {
			personalMessageRecord = new MessageIndexVO();
			personalMessageRecord.setType(1);
			personalMessageRecord.setUnreadCount(unReadCount);
		}
		// 产品通知
		MessageIndexVO productNotice = productAnnouncementMapper.selectMessageIndexInfo(ProductAndAnnouncementType.PRODUCT_NOTICE);
		unReadCount = productAnnouncementMapper.selectUnReadCountByTimeAndType(ProductAndAnnouncementType.PRODUCT_NOTICE, lastReadProductNoticeTime, nowTime);
		if(productNotice != null) {
			productNotice.setType(2);
			productNotice.setUnreadCount(unReadCount);
		}else {
			productNotice = new MessageIndexVO();
			productNotice.setType(2);
			productNotice.setUnreadCount(unReadCount);
		}
		
		// 平台公告
		MessageIndexVO announcement = productAnnouncementMapper.selectMessageIndexInfo(ProductAndAnnouncementType.PLATFORM_ANNOUNCEMENT);
		unReadCount = productAnnouncementMapper.selectUnReadCountByTimeAndType(ProductAndAnnouncementType.PLATFORM_ANNOUNCEMENT, lastReadPlatformAnnouncementTime, nowTime);
		if(announcement != null) {
			announcement.setType(3);
			announcement.setUnreadCount(unReadCount);
		}else {
			announcement = new MessageIndexVO();
			announcement.setType(3);
			announcement.setUnreadCount(unReadCount);
		}
		
		List<MessageIndexVO> list = new ArrayList<>();
		list.add(personalMessageRecord);
		list.add(productNotice);
		list.add(announcement);
		
		return list;
	}

	@Override
	@Transactional
	public void updateToReadByUserId(Long userId) {
		messageRecordMapper.updateToReadByUserId(userId);
	}
}
